<?php
	require_once dirname(dirname(__FILE__)) . '/Midi/loader.php';
    use \Midi\Parsing\FileParser;
    use \Midi\Translating\TextFormatter;
    use \Midi\Translating\Printer;
    
	$uploaddir = dirname(dirname(__FILE__)) . '/Midi/Song/';
	$uploadfile = $uploaddir . basename($_FILES['upfile']['name']);
	
    $file = $uploadfile;
	
    if (move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile)) {
    	echo "File is valid, and was successfully uploaded.\n";
	} else {
    	echo "Possible file upload attack!\n";
	}
	
    $parser = new FileParser();
    $parser->load($file);
	
    $printer = new Printer(new TextFormatter(), $parser);
    $notes = serialize($printer->printAllNote());
    
    $conn = @mysql_connect("localhost","root","") or die("Could not connect: " . mysql_error());
    mysql_select_db("midi",$conn);
    
    
    $file_name = basename($file,".mid");
	$sql = "INSERT INTO files (file_name, content) VALUES ('$file_name','$notes') ";
	mysql_query($sql,$conn) or die("Invailid query: " . mysql_error());   
?>